<?php

namespace Admin\Controller;

use Common\Controller\AdminbaseController;

class GamesysController extends AdminbaseController {
    public function index() {
        $p=1;
        if (isset($_GET['p'])){
            $p=$_GET['p'];
        }
        $queryDesc='order by id';
        
        $postData=array();
        $where='';
        $postData['p']=$p;

        $Model = M();
        $query=$Model->query("SELECT count(id) as count FROM `z_game` ".$where."  ");
        $count=$query[0]['count'];
        $page = $this->page($count, 10, $p);
        $uInfo=$Model->query("SELECT * FROM `z_game` ".$where."  ".$queryDesc." limit ".(($p-1)*10).",10 ");
        $theData=array();
        $timeNow=time();
        $i=0;
        foreach ($uInfo as $v){
            $checkTable = $Model->query("SHOW TABLES LIKE 'y".$v['id']."_puv' ");
            
            $uPv=$uUv=array();
            if (isset($checkTable[0])){
                $uPv=$Model->query("select count(id) as pv from y".$v['id']."_puv  ");
                $uUv=$Model->query("select count(distinct(openid)) as uv from y".$v['id']."_puv  ");
            }
            $gameEndTime=strtotime($v['endtime']);
            $gameStartTime=strtotime($v['starttime']);
            $gameStatusName='进行中';
            if ($timeNow < $gameStartTime){
                $gameStatusName='未开始';
            }
            if ($timeNow > $gameEndTime){
                $gameStatusName='已结束';
            }
            if (0 == $v['is_open']){
                $gameStatusName='还未启用';
            }
            $v['gamestatus']=$gameStatusName;
            $v['pv']=0;
            $v['uv']=0;
            if (isset($uPv[0]['pv'])){
                $v['pv']=$uPv[0]['pv'];
                $v['uv']=$uUv[0]['uv'];
            }
            
            $theData[]=$v;
        }
        $this->assign("postData", $postData);
        $this->assign("page", $page->show('Admin'));
        $this->assign("thedata", $theData);

        $this->display();
    }
    
    public function add() {
        
        
        $this->display();
    }
    
    public function open() {
        if (isset($_GET['id']) && !empty($_GET['id'])){
            $Model = M();
            $Model->execute("update `z_game` set is_open=0  ");
            $Model->execute("update `z_game` set is_open=1 where id='".$_GET['id']."'  ");
            
            $data=file_get_contents("http://climb-game.51wom.com/chuang/?c=create");
            sleep(1);
            if($data){
                header("Location: admin.php?g=Admin&m=Gamesys&a=index ");
            }
        }
    }
    
    public function delete() {
        if (isset($_GET['id']) && !empty($_GET['id'])){
            $Model = M("game", 'z_');
            $data=array();
            if ($Model->delete($_GET['id'])!==false) {
                $this->success("删除成功！");
            } else {
                $this->error("删除失败！");
            } 
        }
    }
  
    public function edit() {
        if (isset($_GET['id'])){
            $id=$_GET['id'];
        }else{
            exit;
        }
        $p=1;
        if (isset($_GET['p'])){
            $p=$_GET['p'];
        }
        $Model = M();
        $thedata=$Model->query("SELECT * FROM `z_game` where id='".$id."' limit 0,1  ");
        $this->assign("p", $p);
        $this->assign("thedata", $thedata[0]);
         
        $this->display();
    }
     
    public function add_post() {
        if (isset($_POST['name']) && !empty($_POST['name'])){
            $Model = M("game", 'z_');
            $data=array();
            $data['name'] = addslashes(trim($_POST['name']));
            $data['starttime'] = addslashes(trim($_POST['starttime']));
            $data['endtime'] = addslashes(trim($_POST['endtime']));
            $data['topnum'] = (int)trim($_POST['topnum']);
            $data['game_bg'] = addslashes(trim($_POST['gamebg']));
            
            $data['sharetitle'] = addslashes(trim($_POST['sharetitle']));
            $data['sharedesc'] = addslashes(trim($_POST['sharedesc']));
            $data['shareimg'] = $_POST['smeta']['thumb'];
            $data['shuoming'] = addslashes($_POST['post']['post_content']);
            $data['createtime'] = date("Y-m-d H:i:s");
            
            if($data['topnum'] < 1){
                $data['topnum'] =1;
            }
            
            $Model->add($data);
            $info=json_encode(array('status'=>'success'));
            echo $info;
        }
    }
    
    public function editpost() {
 
        if (isset($_POST['name']) && isset($_POST['option_id'])){
            $Model = M("game", 'z_');
            $data=array();
            $data['name'] = addslashes(trim($_POST['name']));
            $data['starttime'] = addslashes(trim($_POST['starttime']));
            $data['endtime'] = addslashes(trim($_POST['endtime']));
            $data['game_bg'] = addslashes($_POST['gamebg']);
            $data['sharetitle'] = addslashes(trim($_POST['sharetitle']));
            $data['sharedesc'] = addslashes(trim($_POST['sharedesc']));
            $data['shareimg'] = $_POST['thumb'];
            $data['shuoming'] = $_POST['post']['post_content'];
            $data['createtime'] = date("Y-m-d H:i:s");
            $id=$_POST['option_id'];
            $status=$Model->where('id='.$id)->save($data);
 
            //$sql=$Model->getLastSql();
          
            
            $info=json_encode(array('status'=>'yes'));
            echo $info;
        }
    }
 
}
